package com.controle;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.servlet.http.HttpServletRequest;

import org.primefaces.context.RequestContext;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;

import com.negocio.entidades.CompCurr;
import com.negocio.entidades.Conteudo;
import com.negocio.entidades.Prova;
import com.negocio.entidades.TipoQuestao;
import com.negocio.entidades.Usuario;
import com.negocio.facade.CompCurrRemote;
import com.negocio.facade.ConteudoRemote;
import com.negocio.facade.ProvaRemote;

@SessionScoped
@ManagedBean(name = "profMB")
public class ProfessorMB extends AbstractMB implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@EJB
	private ProvaRemote provaBean;
	@EJB
	private ConteudoRemote conteudoBean;
	@EJB
	private CompCurrRemote compBean;

	private Usuario currUsuario;
	private CompCurr selectedComp;
	private List<CompCurr> compAtuais;
	private List<CompCurr> compTodas;

	private List<Prova> provas;
	private Prova selectedProva;

	private List<Conteudo> conteudos;
	private Conteudo selectedConteudo;
	private Conteudo conteudo;
	private StreamedContent file;
	
	public Conteudo getConteudo() {
		return conteudo;
	}

	public void setConteudo(Conteudo conteudo) {
		if (conteudo.getArquivo() !=null)
			file = new DefaultStreamedContent(new ByteArrayInputStream(conteudo.getArquivo()));
		this.conteudo = conteudo;
	}


	public StreamedContent getFile() {
		return file;
	}

	public void setFile(StreamedContent file) {
		this.file = file;
	}

	@SuppressWarnings("deprecation")
	public ProfessorMB() {
		super();
		FacesContext context = FacesContext.getCurrentInstance();
		HttpServletRequest request = (HttpServletRequest) context
				.getExternalContext().getRequest();

		this.currUsuario = (Usuario) request.getSession().getValue("usu");
		this.compAtuais = new ArrayList<CompCurr>();
		this.compTodas = new ArrayList<CompCurr>();
		for (CompCurr c : currUsuario.getComponentes()) {
			if (c.isStatus()) {
				this.compAtuais.add(c);
			}
			this.compTodas.add(c);
		}

		setSelectedProva(new Prova());
		setSelectedConteudo(new Conteudo());
	}

	/* ===================== GETs e SETs ========================== */
	public List<Conteudo> getConteudos() {
		conteudos = conteudoBean.listar();
		return conteudos;
	}

	public void setConteudos(List<Conteudo> conteudos) {
		this.conteudos = conteudos;
	}

	public Usuario getCurrUsuario() {
		return currUsuario;
	}

	public void setCurrUsuario(Usuario currUsuario) {
		this.currUsuario = currUsuario;
	}

	public List<CompCurr> getCompAtuais() {
		return compAtuais;
	}

	public void setCompAtuais(List<CompCurr> compAtuais) {
		this.compAtuais = compAtuais;
	}

	public List<CompCurr> getCompTodas() {
		return compTodas;
	}

	public void setCompTodas(List<CompCurr> compTodas) {
		this.compTodas = compTodas;
	}

	public CompCurr getSelectedComp() {
		return selectedComp;
	}

	public void setSelectedComp(CompCurr selectedComp) {
		this.selectedComp = selectedComp;
	}

	public List<Prova> getProvas() {
		provas = provaBean.listar();
		return provas;
	}

	public void setProvas(List<Prova> provas) {
		this.provas = provas;
	}

	public Prova getSelectedProva() {
		return selectedProva;
	}

	public void setSelectedProva(Prova selectedProva) {
		this.selectedProva = selectedProva;
	}

	public Conteudo getSelectedConteudo() {
		return selectedConteudo;
	}

	public void setSelectedConteudo(Conteudo selectedConteudo) {
		this.selectedConteudo = selectedConteudo;
	}

	public TipoQuestao[] getTiposQuest() {
		return TipoQuestao.values();
	}

	/* ================================================================= */

	public String showComponente() {
		String url = "/SystemGedJSF/pages/protected/prof/componente.xhtml";
		try {
			FacesContext context = FacesContext.getCurrentInstance();
			context.getExternalContext().redirect(url);
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}

		return url;
	}

	public void doInserirConteudo(ActionEvent actionEvent) throws Exception {
		try {
			conteudoBean.salvar(conteudo);
			setConteudo(new Conteudo());
			RequestContext.getCurrentInstance().execute("novoDialog.hide()");
		} catch (Exception e) {
			displayErrorMessageToUser("Erro ao salvar Conteudo.");
		}
	}

	public void doSalvarProva() throws Exception {
		if (selectedComp.getId() > 0) {
			selectedProva.setCc(selectedComp);
		}
		if (selectedProva.getId() == Long.valueOf(0)) {
			try {
				selectedProva.setDtCadastro(new Date());
				selectedProva.setStatus(false);
				provaBean.salvar(selectedProva);
				setSelectedProva(new Prova());
				RequestContext.getCurrentInstance().execute("dlgProva.hide()");
				RequestContext.getCurrentInstance().update(
						"@(form:listaProvas)");
			} catch (Exception e) {
				displayErrorMessageToUser("Erro ao salvar Prova.");
			}
		} else {
			try {
				provaBean.salvar(selectedProva);
				RequestContext.getCurrentInstance().execute("dlgProva.hide()");
			} catch (Exception e) {
				displayErrorMessageToUser("Erro ao alterar Prova.");
			}
		}
	}

	public void doPrepararProva() throws Exception {
		setSelectedProva(new Prova());
	}

	public void doRemoverProva(Prova prova) {
		try {
			provaBean.excluir(prova.getId());
		} catch (Exception e) {
			displayErrorMessageToUser("Erro ao excluir prova.");
		}
	}
	public void fileUpload(FileUploadEvent event) throws IOException {
			 System.out.println("arquivo... passou aki");
			 file = new DefaultStreamedContent(event.getFile().getInputstream());
			 byte[] arquivos = event.getFile().getContents();
			 conteudo.setArquivo(arquivos);
			 
	}

}
